package org.flow.basic.threadpool;

import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定义线程池拒绝策略
 *
 * @author xl-9527
 * @since 2024/6/23
 **/
public class CustomRejectedExecutionHandler implements RejectedExecutionHandler {

    @Override
    public void rejectedExecution(final Runnable runable, final ThreadPoolExecutor executor) {
        System.err.println("遇到来一些来自线程不够用的情况，不过没关系，我告诉你，你该挨打来！=> " + runable.toString());
        if (runable instanceof ForkJoinTask) {
            ForkJoinTask<?> task = (ForkJoinTask<?>) runable;
            task.cancel(false);
            System.out.println("cancel task");
        }
    }
}
